from openai import OpenAI

# 设置自定义的 API 地址和密钥
client = OpenAI(
    api_key="sk-pusgnznrckhdogxrchddbfzrypvljzjdydjogvnfbprehtkj",
    base_url="https://api.siliconflow.cn/v1"
)

# 初始化对话历史
conversation_history = [
    {
        "content": "你扮演一个猫娘跟我说话，用猫娘的风格回答",
        "role": "system"
    }
]

def chat_with_catgirl():
    print("猫娘已上线！输入 '退出' 结束对话。")
    while True:
        # 获取用户输入
        user_input = input("\n你: ")
        if user_input.lower() in ["退出", "exit", "quit"]:
            print("\n猫娘: 喵~ 再见啦！")
            break
        
        # 将用户输入添加到对话历史中
        conversation_history.append({"content": user_input, "role": "user"})
        
        # 构造请求并获取模型响应（启用流式输出）
        response = client.chat.completions.create(
            model="internlm/internlm2_5-7b-chat",
            stream=True,  # 启用流式输出
            messages=conversation_history
        )
        
        # 流式输出处理
        print("\n猫娘: ", end="", flush=True)  # 提示符
        catgirl_response = ""
        for chunk in response:
            if chunk.choices:  # 确保有内容
                content = chunk.choices[0].delta.content  # 获取增量内容
                if content:
                    print(content, end="", flush=True)  # 实时打印内容
                    catgirl_response += content  # 拼接完整回复
        
        # 将模型的完整回复添加到对话历史中
        conversation_history.append({"content": catgirl_response, "role": "assistant"})

# 启动多轮对话
chat_with_catgirl()